134
Beginner’s Guide to Code Algorithms
134
STEP 3
Get the output into the spreadsheet. Note—the Wait command below to ensure the
page has been given enough time to refresh before extracting the rate
:
:
:
Application.Wait (Now + TimeValue(“0:00:02”))
Set cl_text = ie.document.getElementsByClassName(“DFlfde SwHCTb”)
For Each one In cl_text
Worksheets(“Currency Rates”).Cells(RateRow + x - 1,
1).Value = Format(Date, “MM/DD/YYYY”)
Worksheets(“Currency Rates”).Cells(RateRow + x - 1,
2).Value = Worksheets(“Currency Pairs”).Cells(x, 1).Value
Worksheets(“Currency Rates”).Cells(RateRow + x - 1,
3).Value = Worksheets(“Currency Pairs”).Cells(x, 2).Value
Worksheets(“Currency Rates”).Cells(RateRow + x - 1, 4).Value = one.
innerText
Exit For
Next one
Next x
ie.Quit
Set ie = Nothing
End Sub
9.5 CONCLUSION
This chapter is a vital stepping-stone to understanding the technique of automatically
reading a web page and extracting the information into a spreadsheet.
You learned:
• How to read a web page
• How to identify tags
• How to use browser developer tools
• How to work with HTML
• How to use tags and extract information from inner texts
Connecting Excel with another application is a door to many possibilities, more of
which is discussed in Chapter 10.
Here is an exercise for you—using the web scraping technique described in this
chapter, prepare a list of your ten favorite cities and their maximum and minimum
temperatures for today.